<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
  <title>
    Cannot run program &#34;mysqldump&#34;: CreateProcess error=2, The system cannot find the file specified // 飞狐的部落格
  </title>

  <link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="content-type" content="text/html; charset=utf-8">


<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">

<meta name="description" content="Blog of Rosen Lu">
<meta name="keywords" content="MySQL,Java,">
<meta name="author" content="飞狐">
<meta name="generator" content="Hugo 0.14" />

  <meta property="og:title" content="Cannot run program &#34;mysqldump&#34;: CreateProcess error=2, The system cannot find the file specified" />
<meta property="og:description" content="Blog of Rosen Lu" />
<meta property="og:type" content="website" />
<meta property="og:locale" content="en_US" />
<meta property="og:url" content="http://lucumt.info/posts/can-not-run-program-mysqldump/" />


  
   <link rel="stylesheet" href="http://lucumt.info/css/base-min.css">
   <link rel="stylesheet" href="http://lucumt.info/css/pure-min.css">
  
  
    <link rel="stylesheet" href="http://lucumt.info/css/grids-responsive-min.css">
  
  

  <link rel="stylesheet" href="http://lucumt.info/css/redlounge.css">
  
  <link rel="stylesheet" href="http://lucumt.info/css/googleapis.font.css">
  <link rel="stylesheet" href="http://lucumt.info/css/jqcloud.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
  

  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="http://lucumt.info/touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="http://lucumt.info/lucumt.ico">

  
  <link href="" rel="alternate" type="application/rss+xml" title="飞狐的部落格" />

  <script src="http://lucumt.info//js/jquery.min.js"></script>
  <script src="http://lucumt.info//js/jqcloud-1.0.4.min.js"></script>

    
  
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.6.0/styles/rainbow.min.css">
  
  <script src="http://lucumt.info/js/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>


  

  

  

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', "UA-75123653-1", 'auto');
  ga('send', 'pageview');
</script>




<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cabc0a71f63da092412d82d1aefe7d1c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>
<body>
	
		<div id="nav-to-top">
			<span class="decorative-marker">//</span><a href="#top">返回顶部</a>
		</div>
	

	<div id="layout" class="pure-g">
    
		 <a href="https://github.com/lucumt/ghblog" target="_new">
			<img style="position: absolute; top: 0; right: 0; border: 0;" 
			src="/img/forkme_right_red.png" alt="Fork me on GitHub">
		</a>

    <div class="sidebar pure-u-1 pure-u-md-1-4">
  <div class="header">
    

	
	  <img src="http://lucumt.info/img/photo.jpg" class="profilepic">
	

    <h1 class="brand-title">飞狐的部落格</h1>
    <h2 class="brand-tagline">Rosen Lu</h2>

    <nav class="nav">
      <ul class="nav-list">
        <li class="nav-item"><span class="nav-item-separator">//</span><a href="http://lucumt.info/">Home</a></li>
        
          <li class="nav-item"><span class="nav-item-separator">//</span><a href="http://lucumt.info/about/">About</a></li>
        
          <li class="nav-item"><span class="nav-item-separator">//</span><a href="http://lucumt.info/posts/">Blog</a></li>
        
      </ul>
    </nav>

    
    <div class="social-buttons">
      
        
        <a href="https://github.com/lucumt" target="_blank"><i class='fa fa-github'></i></a>
        
      
        
        <a href="https://plus.google.com/u/0/115794588502118898127/posts" target="_blank"><i class='fa fa-google-plus'></i></a>
        
      
        
        <a href="https://www.facebook.com/rosen.lu.1" target="_blank"><i class='fa fa-facebook'></i></a>
        
      
        
        <a href="http://www.twitter.com/rosenlucumt" target="_blank"><i class='fa fa-twitter'></i></a>
        
      
        
        <a href="mailto:lucumt@gmail.com" target="_blank"><i class='fa fa-mail-forward'></i></a>
        
      
    </div>
    
    
	
	  <hr class="nav-site-separator"/>
	  <nav class="nav">
      <ul class="nav-list">
	    
		   <li class="nav-site"><a href="http://lilydjwg.is-programmer.com/" target="_blank">依云的博客</a></li>
		
		   <li class="nav-site"><a href="http://evilbinary.org/" target="_blank">邪恶二进制</a></li>
		
		   <li class="nav-site"><a href="http://www.wlman.cc/" target="_blank">Consec &#39;s Blog</a></li>
		
		   <li class="nav-site"><a href="http://www.linuxzen.com/" target="_blank">cold&#39;s world</a></li>
		
		   <li class="nav-site"><a href="http://frantic1048.logdown.com/" target="_blank">Frantic log#1048</a></li>
		
	  </ul>
	  </nav>
	
	

  </div>
</div>

	
	

    <div class="content pure-u-1 pure-u-md-3-4">
		<a name="top"></a>
		

		
			
	    
  		<section class="post">
            <h1 class="post-title">
              <a href="http://lucumt.info/posts/can-not-run-program-mysqldump">Cannot run program &#34;mysqldump&#34;: CreateProcess error=2, The system cannot find the file specified</a>
            </h1>
            <h3 class="post-subtitle">
            	
            </h3>
            
            	<span class="post-date">
                	<span class="post-date-day"><sup>3</sup></span><span class="post-date-separator">/</span><span class="post-date-month">Mar</span> <span class="post-date-year">2016</span>
            	</span>
            	
            
            	
            		<span class="post-author-single">By <a class="post-author" href="https://www.linkedin.com/in/%E8%BF%90%E5%BC%BA-%E5%8D%A2-50a08bb5/" target="_new">飞狐</a></span>
            		




            	
            

			
			
				<div class="post-categories">
				
					<a class="post-category post-category-mysql" href="http://lucumt.info//categories/mysql">MySQL</a>
				
					<a class="post-category post-category-java" href="http://lucumt.info//categories/java">Java</a>
				
				</div>
			

			

			

            <p>项目中用到了<code>MySQL</code>数据库的备份功能，通过调用<code>Java</code>程序中的<strong><a href="http://docs.oracle.com/javase/6/docs/api/">Runtime</a></strong>来执行<code>mysqldump</code>命令自动的生成相关的<code>MySQL</code>数据库文件以供恢复之用。相关的代码如下:</p>

<pre><code class="language-java">Runtime runtime = Runtime.getRuntime();
String mysqlCmd = &quot;mysqldump&quot; + &quot; -u&quot; + username + &quot; -p&quot; + password + 
           &quot;  -h &quot; + databaseAddress + &quot; &quot; +databaseName;
Process process = runtime.exec(mysqlCmd);
</code></pre>

<p>但是在客户那里实际使用时，有时候会出现在cmd中<code>MySQL</code>命令可以正常识别但是程序不能正常执行的情况，报错信息如下:</p>

<pre><code class="language-java">java.io.IOException: Cannot run program &quot;mysqldump&quot;: CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
	at java.lang.Runtime.exec(Runtime.java:593)
	at java.lang.Runtime.exec(Runtime.java:431)
	at java.lang.Runtime.exec(Runtime.java:328)
</code></pre>

<p>Google之后，在<strong><a href="http://stackoverflow.com/">Stackoverflow</a></strong>发现两个相关的问题：</p>

<ul>
<li><a href="http://stackoverflow.com/questions/15850548/error-when-backing-up-mysql-database">Error when backing up MYSQL database</a></li>
<li><a href="http://stackoverflow.com/questions/13376132/backup-mysql-database-java-code">backup mysql database java code</a><br /></li>
</ul>

<p>阅读之后，发现上面说问题产生的原因是<code>mysqldump</code>命令无法识别，把<code>mysqldump</code>可执行文件的路径加入PATH环境变量中即可解决问题。但当我在cmd中无论执行<code>mysql</code>或<code>mysqldump</code>命令时，都显示这两个命令可以正常执行：<br />
<img src="https://ooo.0o0.ooo/2016/03/03/56d7e3254c0a0.png" alt="mysql1.png" />
<br />
在cmd中输出PATH环境变量时，也显示<code>MySQL</code>的bin目录已经添加:<br />
<img src="https://ooo.0o0.ooo/2016/03/03/56d7e39ff3ab8.png" alt="mysql2.png" />
<br />
即使重启电脑，上述通过<code>Java</code>备份<code>MySQL</code>的代码还是不能正常执行，但当在cmd中执行<code>mysql</code>、<code>mysqldump</code>命令或输出PATH环境变量时，结果任何上面图片中显示的一致。</p>

<p>这下让我感到很困惑:&nbsp;<strong>通过</strong><code>Java</code><strong>代码来执行</strong><code>mysqldump</code><strong>导出操作时去不能正常执行原因是</strong><code>MySQL</code><strong>的执行路径没有加到PATH环境变量中,但实际检查发现</strong><code>MySQL</code><strong>的环境变量设置正常，在命令行通过</strong><code>mysqldump</code><strong>导出sql文件可以成功操作!</strong>继续在网上搜索该问题的解决方案，得到的答案也都是<code>MySQL</code>的执行路径没有加到PATH环境变量中去，问题依旧。。。</p>

<p>正当我在为这个问题发愁时，测试部门有个同事的新<code>Win7</code>电脑上利用我们的软件执行<code>MySQL</code>备份时也出现了类似的问题，之前我还猜有可能是由于客户服务器的操作系统版本太低或某些DLL文件不存在导致的。但现在居然在刚装好的<code>Win7</code>电脑上也出现此问题，基本可以排除操作系统的问题。由于在我自己的笔记本和台式研发机上都没出现这个问题，无奈之下我只好把同事的电脑拿过来和我自己的电脑进行对比，看看哪里设置不一样。通过<code>Win7</code>中<code>高级系统设置</code>查看PATH环境变量，很快就发现了问题的根源：<code>MySQL</code><strong>的执行路径被设置到了</strong><code>用户变量</code><strong>中的PATH变量里，</strong><code>系统变量</code><strong>中的PATH变量里却没有</strong><code>MySQL</code><strong>的执行路径，而</strong><code>Java</code><strong>代码是匿名执行的，无法获取到</strong><code>用户变量</code>，<strong>只能去</strong><code>系统变量</code><strong>中寻找相关的可执行命令,因而程序会出错！</strong><br />
<img src="https://ooo.0o0.ooo/2016/03/03/56d7ebcbce7fc.png" alt="mysql3.png" />
<br />
这下问题原因变得很清楚了，我们在<code>cmd</code>中执行<code>mysql</code>和<code>mysqldump</code>命令以及输出PATH环境变量时，系统会把当前用户的<code>用户变量</code>中的PATH和操作系统的<code>系统变量</code>中的PATH变量整合到一块，所以我们在cmd中操作时一切正常。但是当我们在<code>Java</code>程序中执行<code>mysqldump</code>命令时，由于<code>Java</code>程序的运行和用户无关，无法获取到<code>用户变量</code>中的PATH值，所以当我们在<code>Java</code>程序中执行<code>mysqldump</code>命令时会出错。这也正好和<strong><a href="http://stackoverflow.com/">Stackoverflow</a></strong>中说明的原因一致。</p>

<p>由于有的电脑上会出现此问题，有的电脑上没有此问题，进一步的深究问题的根源，发现发生问题的电脑和服务器在安装<code>MySQL</code>数据库时都是通过我们自己写的<code>bat</code>脚本来安装的。而<code>bat</code>脚本中设置环境变量的代码如下:</p>

<pre><code class="language-ruby">@echo %Path%
setx PATH &quot;%Path%;C:\INTA\Database\bin;&quot;
@echo %Path%
</code></pre>

<p>问题的关键就在于<strong><em>setx PATH &ldquo;%Path%;C:\INTA\Database\bin;&rdquo;</em></strong>这行代码，这样写的话只会把<code>MySQL</code>的执行路径加入到当前执行该脚本的<code>用户变量</code>中，不会加入到<code>环境变量</code>中。而那些没有出问题的电脑都是我自己手动在<code>系统变量</code>中设置<code>MySQL</code>执行路径的！该问题的解决方法也很简单，在<strong><em>setx</em></strong>后面加上<strong><em>-m</em></strong>即可，这样<code>bat</code>脚本执行时会把<code>MySQL</code>的执行路径写入<code>系统变量</code>的PATH变量中，不会写入<code>用户变量</code>的PATH变量中：</p>

<pre><code class="language-ruby">@echo %Path%
setx -m PATH &quot;%Path%;C:\INTA\Database\bin;&quot;
@echo %Path%
</code></pre>

<p>Orz~<br />
想不到由于一个<strong><em>-m</em></strong>而让自己郁闷了这么久!</p>

	
			

			

			
          </section>
		  
		  <div class="page-link">
			
			<a href="http://lucumt.info/posts/create-website-with-hugo/">//下一篇</a>
			
			&nbsp;
			
			<a class="page-link-right" href="http://lucumt.info/posts/java-concurrency/java-volatile-keyword/">//上一篇</a>
			
	      </div>

          
          	<div id="disqus_thread"></div>
<script type="text/javascript">
    var disqus_shortname = 'rosenlu';
    var disqus_identifier = 'http:\/\/lucumt.info\/posts\/can-not-run-program-mysqldump\/';
    var disqus_title = 'Cannot run program \x22mysqldump\x22: CreateProcess error=2, The system cannot find the file specified';
    var disqus_url = 'http:\/\/lucumt.info\/posts\/can-not-run-program-mysqldump\/';

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
          
        
      <div class="footer">
	<hr class="thin" />
	<div class="pure-menu pure-menu-horizontal pure-menu-open">
		<ul class="footer-menu">
		
			<li><a href="https://github.com/tmaiaroto/hugo-redlounge">Designed by Red Lounge</a></li>
		
		</ul>
	</div>

	<p>&copy; 2017. All rights reserved.</p>
</div>
    </div>
  </div>
	
		<script type="text/javascript">
			onscroll = function() {
			  var toTopVisible = false;
			  var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			  if (scrollTop > 1000) {
			    if (!toTopVisible) {
			      document.getElementById('nav-to-top').style.display = 'block';
			    }
			  } else {
			    if (scrollTop < 1000 || toTopVisible) {
			      document.getElementById('nav-to-top').style.display = 'none';
			    }
			  }
			};
		</script>
	

	

  
</body>
</html>